<?php
class App_Shop_Manager
{
    public static function currentShopId()
    {
        $adminInfo = User::currentAdminUser()->get_user_info();
        return Arr::get($adminInfo, 'shop_id', 0);
    }

    public static function items ($offset, $limit)
    {
        $query = DB::select(
            array('u.realname', 'managerName'),
            's.shopName',
            's.shopIcon',
            's.shortDesc',
            'i.created',
            'i.shopId',
            'i.uid',
            'i.id'
            )
            ->from(array('shop_manager', 'i'))
            ->join(array('user_basic', 'u'), 'LEFT')
            ->on('i.uid', '=', 'u.uid')
            ->join(array('shop_info', 's'), 'LEFT')
            ->on('s.id', '=', 'i.shopId')
            ->where('i.status', '=', 1)
            ->order_by('i.created', 'desc')
            ->execute();

        return $query->as_array();
    }

    public static function create($passport, $password, $realName, $mobile, $shopId)
    {
        $userInfo = array(
            'passport' => $passport,
            'password' => $password,
            'nickname' => $realName,
            'realname' => $realName,
            'mobile'   => $mobile,
            'shop_id'  => $shopId,
        );

        $uid = User::create($userInfo, 'activated', 'merchant');
        return $uid;
    }
}
